import { Label, tween } from "cc";

/**
 * 通用的工具方法
 */
var Common =
{

    spliceOneFromAll(all, one) {
        for (let index = 0; index < all.length; index++) {
            if (all[index] == one) {
                all.splice(index, 1);
                break
            }
        }
    },
    /**
     * 数字滚动增加
     * @param originalNum 初始数字
     * @param curNum 增长数字
     * @param label 节点
     * @param time 持续时间，默认1s
     */
    updatenNumberAnim(originalNum: number = 0, curNum?: number, label?: Label, time: number = 1, callBack?: Function) {
        let obj = { num: originalNum };
        label.string = obj.num + '';
        tween(obj)
            .to(time, { num: curNum }, {
                progress: (start, end, current, t) => {
                    label.string = Math.ceil(start + (end - start) * t) + '';
                    return start + (end - start) * t;
                }
            })
            .call(() => {
                if (callBack)
                    callBack()
            })
            .start();
    },

    getNowTime() {
        // 获取当前日期对象
        const currentDate = new Date();

        // 获取年、月、日、时、分
        const year = currentDate.getFullYear();
        const month = currentDate.getMonth() + 1; // 月份从 0 开始，需要加 1
        const day = currentDate.getDate();
        const hours = currentDate.getHours();
        const minutes = currentDate.getMinutes();

        // 格式化日期
        const formattedDate = `${year}年${month}月${day}日${hours}时${minutes}分`;

        // 输出格式化后的日期
        return formattedDate;
    },

    //获得随机数
    getRandom(minNum: number, maxNum: number) {
        switch (arguments.length) {
            case 1:
                return Math.floor(Math.random() * minNum + 1);
            case 2:
                return Math.floor(Math.random() * (maxNum - minNum + 1) + minNum);
            default:
                return 0;
        }
    },

    /**
  * 时间秒  转换成字符串
  * 格式  00:00:00
  * @param time  秒
  */
    timeTransitionString(time: number, index?: number) {
        let sec = time % 60; //秒
        let min = Math.floor(time / 60) % 60;//分
        let hour = Math.floor(time / 3600); //时
        let str = "";

        switch (index) {
            case 1:
                if (sec < 10) {
                    str += "0" + sec;
                } else {
                    str += sec;
                }
                break;

            case 2:
                if (min < 10) {
                    str += "0" + min + ":";
                } else {
                    str += min + ":";
                }
                if (sec < 10) {
                    str += "0" + sec;
                } else {
                    str += sec;
                }
                break;
            case 3:
                if (hour < 10) {
                    str += "0" + hour + ":";
                } else {
                    str += hour + ":";
                }
                if (min < 10) {
                    str += "0" + min + ":";
                } else {
                    str += min + ":";
                }
                if (sec < 10) {
                    str += "0" + sec;
                } else {
                    str += sec;
                }
                break
        }

        //         if (hour < 10) {
        //             str += "0" + hour + ":";
        //         } else {
        //             str += hour + ":";
        //         }
        //         if (min < 10) {
        //             str += "0" + min + ":";
        //         } else {
        //             str += min + ":";
        //         }
        //         if (sec < 10) {
        //             str += "0" + sec;
        //         } else {
        //             str += sec;
        //         }
        return str;
    },

    /**
    * 获取日期组成的数字
    * 例如：20200921
    */
    getNowTimeInDate(): any {
        let time = new Date()
        let str = "0"
        let year = time.getUTCFullYear().toString()
        let month = (time.getUTCMonth() + 1).toString()
        let day = time.getUTCDate().toString()
        let hour = time.getHours().toString()
        let minutes = time.getUTCMinutes().toString()
        let seconds = time.getUTCSeconds().toString()

        if (month.length < 2) {
            month = str + month
        }
        if (day.length < 2) {
            day = str + day
        }
        if (hour.length < 2) {
            hour = str + hour
        }
        if (minutes.length < 2) {
            minutes = str + minutes
        }
        if (seconds.length < 2) {
            seconds = str + seconds
        }

        let dayString = year + month + day + hour + minutes + seconds
        let dayNumber = parseInt(dayString)
        let dayDetail: any = {
            year: year,
            month: month,
            day: day,
            hour: hour,
            minutes: minutes,
            seconds: seconds,
            dayNumber: dayNumber,
            dayString: dayString
        }
        return dayDetail
    },

    /**
     * 找到数组中的随机数
     * @param arr 
     * @param num 
     * @returns 
     */
    findCloseNum(arr: number[], num: number) {
        var index = 0; // 保存最接近数值在数组中的索引
        var d_value = Number.MAX_VALUE; // 保存差值绝对值，默认为最大数值
        for (var i = 0; i < arr.length; i++) {
            var new_d_value = Math.abs(arr[i] - num); // 新差值
            if (new_d_value <= d_value) { // 如果新差值绝对值小于等于旧差值绝对值，保存新差值绝对值和索引
                if (new_d_value === d_value && arr[i] < arr[index]) { // 如果数组中两个数值跟目标数值差值一样，取大
                    continue;
                }
                index = i;
                d_value = new_d_value;
            }
        }
        return arr[index] // 返回最接近的数值
    }


}

export { Common }